<?php
class messageController extends appController
{
    public function index()
    {
        $this->assign_global();
        $this->display('index.html');
    }
    
    public function sendMessage()
    {
        $info['name'] = yf_empty($_REQUEST['name'],"请输入您的姓名");
        $info['phone'] = yf_empty($_REQUEST['phone'],"请输入您的联系电话");
        $info['content'] = yf_empty($_REQUEST['content'],"请输入您的建议和意见");
        if(hy_check($info['phone'],'m')=="#@_error")
        {
            $this->showMsg(array("error"=>1,"content"=>"请输入合理的手机号码"));
        }
        
        $result = Model("chat")->insert($info,"message");
        if(!$result){
            $this->showMsg(array("error"=>1,"content"=>"提交失败"));
        }else{
            $this->showMsg(array("error"=>0,"content"=>"感谢您的建议和意见"));
        }
        
    }
    public function lantern()
    {
        $this->check_member();
        
        $m = Model("chat");
        $info = $m->get_record($_SESSION['wxid']);
        $this->assign("info", $info);
        //微信分享参数构建
        $jsdk = $m ->get_jsdk();
            
        $this->assign("jsdk", $jsdk);
            
        $this->assign_global();
        
        $this->display("lantern.html");
    }
    
    public function group()
    {
        $this->check_member();
        $m = Model('message');
        //微信分享参数构建
        $jsdk = $m ->get_jsdk();
        $this->assign("wxid", $_SESSION['wxid']);
        $this->assign("jsdk", $jsdk);
        $this->assign_global();
        $this->display('group.html');
    }
    
    
    public function sendGroup()
    {
        $info['wxid'] = yf_empty($_SESSION['wxid'],"请刷新页面重新提交");
        if(!Model('message')->isApply($info['wxid']))
        {
            $res['error'] = 1;
            $res['content'] = "您已提交过一次，无法再次提交";
            $this->showMsg($res);
        }
        $info['name'] = yf_empty($_REQUEST['name'],"请输入您的姓名");
        $info['phone'] = yf_empty($_REQUEST['phone'],"请输入您的联系电话");
        $info['reason'] = yf_empty($_REQUEST['reason'],"请输入您的宣言");
        if(hy_check($info['phone'],'m')=="#@_error")
        {
            $this->showMsg(array("error"=>1,"content"=>"请输入合理的手机号码"));
        }
        $medias = yf_empty($_REQUEST['media'], "请上传至少一张图片。");
        $img = $this->getPic($medias);
        if(!is_array($img))
        {
            $arr[] = $img;
        }
        else
        {
            $arr = $img;
        }
        $info['img_url'] = json_encode($arr);
        $result = Model("message")->insert($info,"group_info");
        if(!$result){
            $this->showMsg(array("error"=>1,"content"=>"资料提交失败"));
        }else{
            $this->showMsg(array("error"=>0,"content"=>"报名成功，请您留意手机短信通知"));
        }
    }
    
    
    public function getPic($medias)
    {
    
        $token = Model('suipai')->getAccessToken();
    
        $pictures = array();
    
        if(!is_array($medias))
        {
            $temp = $medias;
            $medias = array();
            $medias[] = $temp;
        }
    
        foreach ($medias as $k => $v)
        {
            $media_id = $v;
            $url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token."&media_id=".$media_id;
            $img = $k.rand(0, 100).time().".jpg";
    
            $img = $this->GrabImage($url, $img);
    
            $temp = str_replace(ROOT_PATH, "/", $img);
            $pictures[] = $temp;
    
    
        }
        return $pictures;
    
    
    }
    public function GrabImage($url, $filename = "") {
        if($url == ""):return false;endif;
        if($filename == "") {
            $ext = strrchr($url, ".");
            if($ext != ".gif" && $ext != ".jpg"):return false;endif;
            $filename = date("YmdHis").$ext;
        }
        ob_start();
        readfile($url);
        $img = ob_get_contents();
        ob_end_clean();
        $size = strlen($img);
        $file = 'Uploads/img/'.date('Ym');
        if(!is_dir($file))
        {
            mkdir($file);
        }
        $file = $file."/".$filename;
        $fp2 = @fopen($file, "a");
        fwrite($fp2, $img);
        fclose($fp2);
        return $file;
    }
    
    public function groupnew()
    {
        $this->check_member();
        $m = Model('message');
        //微信分享参数构建
        $jsdk = $m ->get_jsdk();
        $this->assign("wxid", $_SESSION['wxid']);
        $this->assign("jsdk", $jsdk);
        $this->assign_global();
        $this->display('group2.html');
    }
    
    public function sendGroup2()
    {
        $info['wxid'] = yf_empty($_SESSION['wxid'],"请刷新页面重新提交");
        if(!Model('message')->isApply($info['wxid']))
        {
            $res['error'] = 1;
            $res['content'] = "您已提交过，无法再次提交";
            $this->showMsg($res);
        }
        $info['name'] = yf_empty($_REQUEST['name'],"请输入您的姓名");
        $info['phone'] = yf_empty($_REQUEST['phone'],"请输入您的联系电话");
        if(hy_check($info['phone'],'m')=="#@_error")
        {
            $this->showMsg(array("error"=>1,"content"=>"请输入合理的手机号码"));
        }
        $result = Model("message")->insert($info,"group_info");
        if(!$result){
            $this->showMsg(array("error"=>1,"content"=>"资料提交失败"));
        }else{
            $this->showMsg(array("error"=>0,"content"=>"报名成功，请您留意手机短信通知"));
        }
    }
    public function parlour()
    {
        $this->check_member();
        $m = Model('message');
        //微信分享参数构建
        $jsdk = $m ->get_jsdk();
        $this->assign("wxid", $_SESSION['wxid']);
        $this->assign("jsdk", $jsdk);
        $this->assign_global();
        $this->display("parlour.html");
    }
    
    public function child()
    {
        $this->check_member();
        $m = Model('message');
        //微信分享参数构建
        $jsdk = $m ->get_jsdk();
        $this->assign("wxid", $_SESSION['wxid']);
        $this->assign("jsdk", $jsdk);
        $this->assign_global();
        $this->display("child.html");
    }
    
    public function ajax_parlour()
    {
        $m = Model("message");
        if(!intval($_SESSION['wxid'])>0){
            $this->showMsg(array("error"=>1,"content"=>"请刷新后重新登录填写资料"));
            exit();
        }
        if(!$m->isApplyParlour($_SESSION['wxid'])){//已报名
            $this->showMsg(array("error"=>1,"content"=>"您已报名，无法再次提交"));
            exit();
        }
        $info['name'] = trim(yf_empty($_REQUEST['name'],"姓名不可为空"));
        $info['sex'] = trim(yf_empty($_REQUEST['sex'],"性别不可为空"));
        $info['age'] = intval(yf_empty($_REQUEST['age'],"年龄不可为空"));
        if($info['age']<1||$info['age']>99){
            $this->showMsg(array("error"=>1,"content"=>"年龄必须大于1小于99"));
            exit();
        }
        if($info['sex']!="男"&&$info['sex']!="女"){
            $this->showMsg(array("error"=>1,"content"=>"性别请填写“男”或“女”"));
            exit();
        }
        $info['company'] = trim(yf_empty($_REQUEST['company'],"公司不可为空"));
        $info['job'] = trim(yf_empty($_REQUEST['job'],"职务不可为空"));
        $info['tel'] = trim(yf_empty($_REQUEST['tel'],"联系电话不可为空"));
        if(hy_check($info['tel'],"m") == '#@_error')
        {
            $this->showMsg(array("error"=>1,"content"=>"请输入正确的手机号码"));
            exit();
        }
        $info['wxid'] = intval($_SESSION['wxid']);
        $info['addtime'] = yf_time();
        $result = $m->insert($info,"parlour");
        if($result){
            $this->showMsg(array("error"=>0,"content"=>"提交成功！"));
        }else{
            $this->showMsg(array("error"=>1,"content"=>"提交失败"));
        }
    }
    public function ajax_child()
    {
        $m = Model("message");
        if(!intval($_SESSION['wxid'])>0){
            $this->showMsg(array("error"=>1,"content"=>"请刷新后重新登录填写资料"));
            exit();
        }
        if(!$m->isApplyParlour($_SESSION['wxid'])){//已报名
            $this->showMsg(array("error"=>1,"content"=>"您已报名，无法再次提交"));
            exit();
        }
        $info['name'] = trim(yf_empty($_REQUEST['name'],"姓名不可为空"));
        $info['tel'] = trim(yf_empty($_REQUEST['phone'],"联系电话不可为空"));
        if(hy_check($info['tel'],"m") == '#@_error')
        {
            $this->showMsg(array("error"=>1,"content"=>"请输入正确的手机号码"));
            exit();
        }
        $info['company'] = trim(yf_empty($_REQUEST['company'],"单位不可为空"));
        $info['job'] = trim(yf_empty($_REQUEST['job'],"职务不可为空"));
        $info['email'] = trim(yf_empty($_REQUEST['email'],"邮箱不可为空"));
        if(hy_check($info['email'],"e") == '#@_error')
        {
            $this->showMsg(array("error"=>1,"content"=>"请输入正确的电子邮箱"));
            exit();
        }
        $info['wxid'] = intval($_SESSION['wxid']);
        $info['add_time'] = yf_time();
        $result = $m->insert($info,"parlour2");
        if($result){
            $this->showMsg(array("error"=>0,"content"=>"提交成功！"));
        }else{
            $this->showMsg(array("error"=>1,"content"=>"提交失败"));
        }
    }
    
    public function comment()
    {
        $this->check_member();
        $m = Model('message');
        //微信分享参数构建
        $jsdk = $m ->get_jsdk();
        $this->assign("wxuser", json_encode($m -> getRecord($_SESSION['wxid'])));
        $this->assign("wxid", $_SESSION['wxid']);
        $result = $m -> getCommentMobile();
        $this->assign("comments", $result['comment']);
        $this->assign("count", $result['count']);
        $this->assign("jsdk", $jsdk);
        $this->assign_global();
        $this->display("parlour_comment.html");
    }
    public function comment_test()
    {
        $_SESSION['wxid'] = 23498;
        $this->check_member();
        $m = Model('message');
        //微信分享参数构建
        $jsdk = $m ->get_jsdk();
        $this->assign("wxuser", json_encode($m -> getRecord($_SESSION['wxid'])));
        $this->assign("wxid", $_SESSION['wxid']);
        $result = $m -> getCommentMobile();
        $this->assign("comments", $result['comment']);
        $this->assign("count", $result['count']);
        $this->assign("jsdk", $jsdk);
        $this->assign_global();
        $this->display("parlour_comment.html");
    }
    public function getCommentParlour()
    {
        $m = Model("message");
        $result = $m -> getCommentParlour();
        $this->showMsg($result);
    }
    
    
    public function saveComment()
    {
        $m = Model('message');
        $info['comment'] = yf_empty($_POST['text'],'请写下你的精彩评论');
        $info['wxid'] = yf_empty($_POST['wxid'],-1,true);
        $info['status'] = 0;
        $info['create_time'] = yf_time();
        $res = $m -> insert($info,"parlour_comment");
        $count = $m -> getCount();
        if($res)
        {
            $this->showMsg(array("error"=>0,"content"=>"提交成功！","count" => $count));
        }
        $this->showMsg(array("error"=>1,"content"=>"提交失败"));
    }
    
//     public function test()
//     {
//         $this->check_member2();
//         echo isset($_GET['sn_code'])?$_GET['sn_code']:$_GET['code'];echo "<br/>";
//         echo "成功了～～～";
//     }

      public function guangfu()
      {
          $this->check_member();
          $m = Model('message');
          //微信分享参数构建
          $jsdk = $m ->get_jsdk();
          $this->assign("wxid", $_SESSION['wxid']);
          $this->assign("jsdk", $jsdk);
          $this->assign_global();
          $this->display("guangfu.html");
      }
    
      public function ajax_guangfu()
      {
          $m = Model("message");
          if(!intval($_SESSION['wxid'])>0){
              $this->showMsg(array("error"=>1,"content"=>"请刷新后重新登录填写资料"));
              exit();
          }
          if(!$m->isApplyGuangfu($_SESSION['wxid'])){//已报名
              $this->showMsg(array("error"=>1,"content"=>"您已报名，无法再次提交"));
              exit();
          }
          $info['name'] = trim(yf_empty($_REQUEST['name'],"姓名不可为空"));
          $info['phone'] = trim(yf_empty($_REQUEST['phone'],"联系电话不可为空"));
          if(hy_check($info['phone'],"m") == '#@_error')
          {
              $this->showMsg(array("error"=>1,"content"=>"请输入正确的手机号码"));
              exit();
          }
          $info['number'] = intval(yf_empty($_REQUEST['number'],"请填写报名的人数"));
          if(!$info['number']>0){
              $this->showMsg(array("error"=>1,"content"=>"报名人数需大于0"));
          }
          $info['wxid'] = intval($_SESSION['wxid']);
          $info['add_time'] = yf_time();
          $result = $m->insert($info,"guangfu");
          if($result){
              $this->showMsg(array("error"=>0,"content"=>"报名成功！"));
          }else{
              $this->showMsg(array("error"=>1,"content"=>"报名失败"));
          }
      }
      
}